﻿using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using Microsoft.CSharp;

namespace Sharp.Infrastructure
{
    /// <summary>
    /// 取得服务端信息
    /// </summary>
    public class WebServer
    {
        #region 穿过代理服务器取远程用户真实IP地址
        /// <summary>
        /// 穿过代理服务器取远程用户真实IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetIP()
        {
            return GetIP(HttpContext.Current);
        }

        public static string GetIP(HttpContext current)
        {
            try
            {
                string ip = current.Request.ServerVariables["HTTP_VIA"] != null ? current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : current.Request.ServerVariables["REMOTE_ADDR"];
                ip = ip.Replace("::1", "127.0.0.1");

                return ip;
            }
            catch
            {
                return "0.0.0.0";
            }
        }
        #endregion

        #region 测试服务器支持的对象
        /// <summary>
        /// 测试服务器支持的对象
        /// </summary>
        /// <param name="objectName">对象的名称</param>
        /// <returns>bool</returns>
        public bool CheckObject(string objectName)
        {
            try
            {
                HttpContext.Current.Server.CreateObject(objectName);
                return (true);
            }
            catch
            {
                return (false);
            }                          
        }
        #endregion

        #region 取得URL里面的网页名称
        /// <summary>
        /// 取得URL里面的网页名称
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetFileName(string url)
        {
            try
            {
                url = url.Replace(@"\", "/");
                int index = url.LastIndexOf('/') + 1;//移除前没有用的字符，只保留文件名和后缀
                return url.Remove(0, index);
            }
            catch
            {
                return url;
            }
        } 
        #endregion

        #region 获得绝对路径
        /// <summary>
        /// 获得绝对路径
        /// </summary>
        /// <param name="path">相对路径</param>
        /// <param name="args"></param>
        /// <returns>绝对路径</returns>
        public static string MapPath(string path, params Object[] args)
        {
            if (HttpContext.Current != null)
            {
                if (args.Length == 0)
                    return HttpContext.Current.Server.MapPath(path);
                else
                {
                    return HttpContext.Current.Server.MapPath(string.Format(path, args));
                }
            }
            else //非web程序引用
            {
                if (args.Length == 0)
                {
                    path = path.Replace("\\", "/");
                }
                else
                {
                    path = string.Format(path.Replace("\\", "/"), args);
                }
                path = path.TrimStart('~');
                path = path.TrimStart('/');
                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
            }
        } 
        #endregion
       
        /// <summary>
        /// 判断当前页面是否接收到了Post请求
        /// </summary>
        /// <returns>是否接收到了Post请求</returns>
        public static bool IsPost
        {
            get
            {
                return HttpContext.Current.Request.HttpMethod.Equals("POST");
            }
        }
        /// <summary>
        /// 判断当前页面是否接收到了Get请求
        /// </summary>
        /// <returns>是否接收到了Get请求</returns>
        public static bool IsGet
        {
            get
            {
                return HttpContext.Current.Request.HttpMethod.Equals("GET");
            }
        }
  
    }
}